﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.KhuyenMaiTangHang
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        #endregion

        #region khởi tạo

        public bool kiemtra = false;
        bool checkClose = false;
        Entities.KhuyenMaiTangHang khuyenmaitanghang;
        Entities.KhuyenMaiTangHang temp;
        Entities.ChiTietKhuyenMaiTangHang[] chitietkhuyenmaitanghang;
        Entities.KhuyenMaiTangHang[] tempcheck;
        Entities.ChiTietKhuyenMaiTangHang[] chitiettempcheck;
        string makho = "";
        string hanhDong;

        public frmXuLy()
        {
            InitializeComponent();
        }

        public frmXuLy(Entities.KhuyenMaiTangHang[] tempcheck)
        {
            InitializeComponent();
            try
            {
                txtTenDotKhuyenMai.Focus();
                Entities.CaiDatKho kho = Entitystatic.ViewFile();
                makho = kho.MaKho;
                hanhDong = "Insert";
                this.tempcheck = tempcheck;
                this.chitiettempcheck = Common.StaticVariable.InterfaceObject.SelectChiTietKhuyenMaiTangHang();
                makhuyenmai = getID();
                dgvHienThi.DataSource = new Entities.ChiTietKhuyenMaiTangHang[0];
                fix();
            }
            catch
            {
            }
        }

        public frmXuLy(Entities.KhuyenMaiTangHang khuyenmaitanghang, Entities.KhuyenMaiTangHang[] tempcheck)
        {
            InitializeComponent();
            if (khuyenmaitanghang.GiaTriTrangThai == "Đang sử dụng" || khuyenmaitanghang.GiaTriTrangThai == "Đã sử dụng")
            {
                btnGhiLai.Enabled = false;
            }
            try
            {
                txtTenDotKhuyenMai.Focus();
                // gán giá trị
                this.tempcheck = tempcheck;
                temp = khuyenmaitanghang;
                this.chitiettempcheck = Common.StaticVariable.InterfaceObject.SelectChiTietKhuyenMaiTangHang();
                hanhDong = "Update";

                // hiển thị chi tiết
                chitietkhuyenmaitanghang = HienThiChiTiet(khuyenmaitanghang);
                dgvHienThi.DataSource = chitietkhuyenmaitanghang;
                fix();
                XuLyDuLieu(khuyenmaitanghang);
            }
            catch
            {
            }
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.TenHangDuocKhuyenmai].Visible = true;
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.TenHangKhuyenMai].Visible = true;
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.SoLuongDuocKhuyenMai].Visible = true;
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.SoLuongKhuyenMai].Visible = true;
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.MaHangDuocKhuyenmai].Visible = true;
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.MaHangKhuyenMai].Visible = true;
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.MaHangKhuyenMai].HeaderText = "Mã Hàng Khuyến Mại";
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.MaHangDuocKhuyenmai].HeaderText = "Mã Hàng Được Khuyến Mại";
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.SoLuongKhuyenMai].HeaderText = "Số Lượng khuyến mại";
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.SoLuongDuocKhuyenMai].HeaderText = "Số Lượng Được Khuyến Mại";
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.TenHangKhuyenMai].HeaderText = "Tên Hàng Khuyến mại";
            dgvHienThi.Columns[Common.Constants.ChiTietKhuyenMaiTangHang.TenHangDuocKhuyenmai].HeaderText = "Tên Hàng Được Khuyến Mại";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }

        #endregion

        #region hàng khuyến mại
        Entities.ChiTietKhuyenMaiTangHang[] hienthi;
        Entities.ChiTietKhuyenMaiTangHang[] templv;
        Entities.ChiTietKhuyenMaiTangHang save;
        string mahanghoakm = "";
        string mahanghoaduockm = "";

        void ThemHangKhuyenMai()
        {
            try
            {

                int sohangtrongbang;
                hienthi = (Entities.ChiTietKhuyenMaiTangHang[])dgvHienThi.DataSource;
                // check hàng khuyến mại
                if (!CheckHangKhuyenMai())
                {
                    MessageBox.Show(Common.MSG.MSG24(txtMaHangHoaKM.Text, makhuyenmaitrung), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // list view = null
                if (hienthi == null)
                {
                    string soluongkm = "0";
                    if (txtSoLuongKM.Text == "")
                        soluongkm = "1";
                    else
                        soluongkm = txtSoLuongKM.Text;

                    string soluongduockm = "0";
                    if (txtSoLuongDuocKM.Text == "")
                        soluongduockm = "1";
                    else
                        soluongduockm = txtSoLuongDuocKM.Text;

                    templv = new Entities.ChiTietKhuyenMaiTangHang[1];
                    templv[0] = new Entities.ChiTietKhuyenMaiTangHang("Insert", 0, makhuyenmai, txtMaHangHoaKM.Text, lbTenHangHoaKM.Text,
                        int.Parse(soluongkm), txtMaHangHoaDuocKM.Text, lbTenHangHoaDuocKM.Text, int.Parse(soluongduockm));
                    dgvHienThi.DataSource = templv;
                    fix();
                    return;
                }

                sohangtrongbang = hienthi.Length;
                // số lượng bảng ghi khác 0
                if (sohangtrongbang != 0)
                {
                    for (int j = 0; j < sohangtrongbang; j++)
                    {
                        // nếu tìm được mã đã có trong list view
                        if (mahanghoakm == hienthi[j].MaHangKhuyenMai && mahanghoaduockm == hienthi[j].MaHangDuocKhuyenmai)
                        {
                            templv = new Entities.ChiTietKhuyenMaiTangHang[sohangtrongbang];
                            break;
                        }
                        // nếu không tìm được
                        else
                            templv = new Entities.ChiTietKhuyenMaiTangHang[sohangtrongbang + 1];
                    }
                }
                // số lượng bảng ghi = 0
                else
                    templv = new Entities.ChiTietKhuyenMaiTangHang[sohangtrongbang + 1];

                // nếu số lượng bảng ghi khác 0
                if (templv.Length != 0)
                {
                    try
                    {
                        // số lượng bảng ghi = số lượng row list view
                        if (templv.Length == hienthi.Length)
                        {
                            for (int j = 0; j < templv.Length; j++)
                            {

                                string soluongkm = "0";
                                if (txtSoLuongKM.Text == "")
                                    soluongkm = "1";
                                else
                                    soluongkm = txtSoLuongKM.Text;

                                string soluongduockm = "0";
                                if (txtSoLuongDuocKM.Text == "")
                                    soluongduockm = "1";
                                else
                                    soluongduockm = txtSoLuongDuocKM.Text;

                                // nếu mã = mã trong list view
                                if (mahanghoakm == hienthi[j].MaHangKhuyenMai && mahanghoaduockm == hienthi[j].MaHangDuocKhuyenmai)
                                {
                                    int soluongkmcu = hienthi[j].SoLuongKhuyenMai;
                                    int soluongduockmcu = hienthi[j].SoLuongDuocKhuyenMai;

                                    int soluongkmhientai = soluongkmcu + int.Parse(soluongkm);
                                    int soluongduockmhientai = soluongduockmcu + int.Parse(soluongduockm);

                                    templv[j] = new Entities.ChiTietKhuyenMaiTangHang("Insert", 0, makhuyenmai, txtMaHangHoaKM.Text, lbTenHangHoaKM.Text,
                                            soluongkmhientai, txtMaHangHoaDuocKM.Text, lbTenHangHoaDuocKM.Text, soluongduockmhientai);

                                }
                                // nếu mã không = mã trong list view
                                else
                                    templv[j] = new Entities.ChiTietKhuyenMaiTangHang("Insert", 0, makhuyenmai, hienthi[j].MaHangKhuyenMai, hienthi[j].TenHangKhuyenMai,
                                 hienthi[j].SoLuongKhuyenMai, hienthi[j].MaHangDuocKhuyenmai, hienthi[j].TenHangDuocKhuyenmai, hienthi[j].SoLuongDuocKhuyenMai);

                            }
                        }
                        // số lượng bảng ghi không = số lượng row list view
                        else
                        {
                            for (int j = 0; j < templv.Length; j++)
                            {
                                string soluongkm = "0";
                                if (txtSoLuongKM.Text == "")
                                    soluongkm = "1";
                                else
                                    soluongkm = txtSoLuongKM.Text;

                                string soluongduockm = "0";
                                if (txtSoLuongDuocKM.Text == "")
                                    soluongduockm = "1";
                                else
                                    soluongduockm = txtSoLuongDuocKM.Text;

                                // lấy lại số bảng ghi trên list view
                                if (j < templv.Length - 1)
                                    templv[j] = new Entities.ChiTietKhuyenMaiTangHang("Insert", 0, makhuyenmai, hienthi[j].MaHangKhuyenMai, hienthi[j].TenHangKhuyenMai,
                                     hienthi[j].SoLuongKhuyenMai, hienthi[j].MaHangDuocKhuyenmai, hienthi[j].TenHangDuocKhuyenmai, hienthi[j].SoLuongDuocKhuyenMai);
                                // bảng ghi cuối cùng là mới insert vào
                                else
                                    templv[templv.Length - 1] = new Entities.ChiTietKhuyenMaiTangHang("Insert", 0, makhuyenmai, txtMaHangHoaKM.Text, lbTenHangHoaKM.Text,
                                    int.Parse(soluongkm), txtMaHangHoaDuocKM.Text, lbTenHangHoaDuocKM.Text, int.Parse(soluongduockm));

                            }
                        }
                    }
                    catch
                    {
                        string soluongkm = "0";
                        if (txtSoLuongKM.Text == "")
                            soluongkm = "1";
                        else
                            soluongkm = txtSoLuongKM.Text;

                        string soluongduockm = "0";
                        if (txtSoLuongDuocKM.Text == "")
                            soluongduockm = "1";
                        else
                            soluongduockm = txtSoLuongDuocKM.Text;

                        templv[templv.Length - 1] = new Entities.ChiTietKhuyenMaiTangHang("Insert", 0, makhuyenmai, txtMaHangHoaKM.Text, lbTenHangHoaKM.Text,
                                    int.Parse(soluongkm), txtMaHangHoaDuocKM.Text, lbTenHangHoaDuocKM.Text, int.Parse(soluongduockm));
                    }
                    dgvHienThi.DataSource = templv;
                    fix();
                }
            }
            catch
            {
                return;
            }
            finally
            {
                makhuyenmaitrung = "";
            }
        }

        bool CheckHangKhuyenMai()
        {
            try
            {
                foreach (Entities.KhuyenMaiTangHang item in tempcheck)
                {
                    if (item.GiaTriTrangThai == "Đang sử dụng" || item.GiaTriTrangThai == "Sẽ sử dụng" && item.MaKhuyenMaiTangHang != item.MaKhuyenMaiTangHang)
                    {
                        // check chi tiết ở list view
                        if (chitietkhuyenmaitanghang != null)
                        {
                            foreach (Entities.ChiTietKhuyenMaiTangHang item1 in chitietkhuyenmaitanghang)
                            {
                                if (item.MaKhuyenMaiTangHang == item1.MaKhuyenMaiTangHang)
                                {
                                    if (item1.MaHangKhuyenMai == txtMaHangHoaKM.Text && item1.SoLuongKhuyenMai.ToString() != txtSoLuongKM.Text)
                                    {
                                        return false;
                                    }
                                }
                            }
                        }
                        // check chi tiết ở database
                        foreach (Entities.ChiTietKhuyenMaiTangHang item1 in chitiettempcheck)
                        {
                            if (item.MaKhuyenMaiTangHang == item1.MaKhuyenMaiTangHang)
                            {
                                if (item1.MaHangKhuyenMai == txtMaHangHoaKM.Text && item1.SoLuongKhuyenMai.ToString() != txtSoLuongKM.Text)
                                {
                                    makhuyenmaimoi = item1.MaKhuyenMaiTangHang;
                                    return false;
                                }
                            }
                        }

                    }
                }
                return true;
            }
            catch
            {
                return false;

            }
        }

        void XoaHangKhuyenMai()
        {
            try
            {
                hienthi = (Entities.ChiTietKhuyenMaiTangHang[])dgvHienThi.DataSource;
                // list view = null
                if (hienthi == null)
                {
                    return;
                }
                // obj = null
                if (save == null)
                {
                    return;
                }
                templv = new Entities.ChiTietKhuyenMaiTangHang[hienthi.Length - 1];
                int sotang = 0;
                for (int j = 0; j < hienthi.Length; j++)
                {
                    if (hienthi[j].MaHangKhuyenMai != save.MaHangKhuyenMai || hienthi[j].MaHangDuocKhuyenmai != save.MaHangDuocKhuyenmai)
                    {
                        templv[sotang] = new Entities.ChiTietKhuyenMaiTangHang("Insert", 0, makhuyenmai, hienthi[j].MaHangKhuyenMai, hienthi[j].TenHangKhuyenMai,
                            hienthi[j].SoLuongKhuyenMai, hienthi[j].MaHangDuocKhuyenmai, hienthi[j].TenHangDuocKhuyenmai, hienthi[j].SoLuongDuocKhuyenMai);
                        sotang++;
                    }
                    else
                    {
                        txtMaHangHoaKM.Text = hienthi[j].MaHangKhuyenMai;
                        txtSoLuongKM.Text = hienthi[j].SoLuongKhuyenMai.ToString();
                        lbTenHangHoaKM.Text = hienthi[j].TenHangKhuyenMai;
                        txtMaHangHoaDuocKM.Text = hienthi[j].MaHangDuocKhuyenmai;
                        txtSoLuongDuocKM.Text = hienthi[j].SoLuongDuocKhuyenMai.ToString();
                        lbTenHangHoaDuocKM.Text = hienthi[j].TenHangDuocKhuyenmai;
                    }
                }
                dgvHienThi.DataSource = templv;
                fix();
                save = null;
            }
            catch
            {
            }
        }
        #endregion

        #region get id
        // xử lý ID
        public string getID()
        {
            try
            {
                string str = "";
                Entities.GetID layid = new Entities.GetID();
                layid.TableName = "KhuyenMaiTangHang";
                Entities.GetID lay = Common.StaticVariable.InterfaceObject.SelectID(layid);
                if (lay.ColumnName == null)
                {
                    return "KMTH_0001";
                }
                str = Common.Utilities.ProcessID(lay.ColumnName);
                return str;
            }
            catch
            {
                return "KMTH_0001";
            }
        }
        #endregion

        #region Validate
        public DateTime batdau, ketthuc;
        public bool Validate()
        {
            if (txtTenDotKhuyenMai.Text == "")
            {
                MessageBox.Show(Common.MSG.MSG13(lbTenDotKhuyenMai.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                txtTenDotKhuyenMai.Focus();
                return false;
            }

            try
            {
                batdau = DateTime.Parse(dtpkNgayBatDau.Text);
            }
            catch
            {
                MessageBox.Show(Common.MSG.MSG13(lbNgayBatDau.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                dtpkNgayBatDau.Focus();
                return false;
            }
            try
            {
                ketthuc = DateTime.Parse(dtpkNgayKetThuc.Text);
            }
            catch
            {
                MessageBox.Show(Common.MSG.MSG13(lbNgayKetThuc.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                dtpkNgayKetThuc.Focus();
                return false;
            }
            if (batdau.Date > ketthuc.Date)
            {
                MessageBox.Show(Common.MSG.MSG12(lbNgayBatDau.Text, lbNgayKetThuc.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                dtpkNgayBatDau.Focus();
                return false;
            }
            return true;
        }

        public bool ValidateDetails()
        {
            if (txtMaHangHoaDuocKM.Text == "<F4 - Tra cứu>")
            {
                MessageBox.Show(Common.MSG.MSG13(lbMaHangHoaDuocKM.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                txtMaHangHoaDuocKM.Focus();
                return false;
            }
            if (txtMaHangHoaKM.Text == "<F4 - Tra cứu>")
            {
                MessageBox.Show(Common.MSG.MSG13(lbMaHangHoaKM.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                txtMaHangHoaKM.Focus();
                return false;
            }
            if (txtSoLuongDuocKM.Text == "")
                txtSoLuongDuocKM.Text = "1";
            if (txtSoLuongKM.Text == "")
                txtSoLuongKM.Text = "1";
            return true;
        }

        public string makhuyenmai = "";
        public void XuLyDuLieu(Entities.KhuyenMaiTangHang khuyenmaitanghang)
        {
            try
            {
                makhuyenmai = khuyenmaitanghang.MaKhuyenMaiTangHang;
                dtpkNgayBatDau.Text = khuyenmaitanghang.NgayBatDau.ToShortDateString();
                dtpkNgayKetThuc.Text = khuyenmaitanghang.NgayKetThuc.ToShortDateString();
                txtTenDotKhuyenMai.Text = khuyenmaitanghang.TenDotKhuyenMai;
            }
            catch
            {
            }
        }

        public Entities.KhuyenMaiTangHang XuLyObject(string hanhDong)
        {
            try
            {
                if (Validate())
                {
                    khuyenmaitanghang = new Entities.KhuyenMaiTangHang();
                    khuyenmaitanghang.HanhDong = hanhDong;
                    khuyenmaitanghang.MaKhuyenMaiTangHang = makhuyenmai;
                    khuyenmaitanghang.NgayBatDau = DateTime.Parse(dtpkNgayBatDau.Text);
                    khuyenmaitanghang.NgayKetThuc = DateTime.Parse(dtpkNgayKetThuc.Text);
                    khuyenmaitanghang.TenDotKhuyenMai = txtTenDotKhuyenMai.Text;
                    khuyenmaitanghang.NgayLap = Entitystatic.date;
                    return khuyenmaitanghang;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        public Entities.ChiTietKhuyenMaiTangHang[] HienThiChiTiet(Entities.KhuyenMaiTangHang khuyenmaitanghang)
        {
            Entities.ChiTietKhuyenMaiTangHang[] temp = Common.StaticVariable.InterfaceObject.SelectChiTietKhuyenMaiTangHang();
            return (Entities.ChiTietKhuyenMaiTangHang[])temp.Where(p => p.MaKhuyenMaiTangHang == khuyenmaitanghang.MaKhuyenMaiTangHang).ToArray();
        }

        public Entities.ChiTietKhuyenMaiTangHang[] HienThi()
        {
            Entities.ChiTietKhuyenMaiTangHang[] chitietkhuyenmaitanghang = Common.StaticVariable.InterfaceObject.SelectChiTietKhuyenMaiTangHang();
            return chitietkhuyenmaitanghang;
        }

        #endregion

        #region Ghi lại
        public void GhiLai()
        {
            if (kiemtra)
                Sua();
            else
                Them();
        }

        #region thêm
        void Them()
        {
            try
            {
                khuyenmaitanghang = XuLyObject("Insert");
                chitietkhuyenmaitanghang = (Entities.ChiTietKhuyenMaiTangHang[])dgvHienThi.DataSource;
                tempcheck = new frmQuanLy().HienThi(Entitystatic.date);
                chitiettempcheck = HienThi();
                if (batdau.Date < Entitystatic.date.Date)
                {
                    MessageBox.Show(Common.MSG.MSG12("Ngày hiện tại", lbNgayBatDau.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    dtpkNgayBatDau.Focus();
                    return;
                }
                // check hàng khuyến mại
                if (!CheckHangHoaKhuyenMai())
                {
                    MessageBox.Show(Common.MSG.MSG24(txtMaHangHoaKM.Text, makhuyenmaitrung), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check ngày khuyến mại
                if (!CheckNgayKhuyenMaiTangHang())
                {
                    MessageBox.Show(Common.MSG.MSG29(Common.Utilities.XuLy(2, dtpkNgayBatDau.Text), Common.Utilities.XuLy(2, dtpkNgayKetThuc.Text)), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }

                // check object
                if (khuyenmaitanghang == null)
                {
                    return;
                }
                // check object
                if (chitietkhuyenmaitanghang == null || chitietkhuyenmaitanghang.Length == 0)
                {
                    MessageBox.Show(Common.MSG.MSG23(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check conflic insert                
                if (!CheckConflicInsert(khuyenmaitanghang))
                {
                    MessageBox.Show(Common.MSG.MSG5(khuyenmaitanghang.MaKhuyenMaiTangHang), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    GetData(true);
                    makhuyenmai = makhuyenmaimoi;
                    return;
                }
                // check insert
                khuyenmaitanghang.TaiKhoan = UtilitiesVariable.User;
                khuyenmaitanghang.ChiTietKhuyenMaiTangHang = chitietkhuyenmaitanghang;

                if (Common.StaticVariable.InterfaceObject.InsertUpdateKhuyenMaiTangHang(khuyenmaitanghang) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG15(khuyenmaitanghang.MaKhuyenMaiTangHang), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    Common.StaticVariable.InterfaceObject.DeleteKhuyenMaiTangHang(khuyenmaitanghang);
                    return;
                }

                checkClose = true;
                khuyenmaitanghang = null;
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Check Conflic Insert
        string makhuyenmaimoi;
        public bool CheckConflicInsert(Entities.KhuyenMaiTangHang khuyenmaitanghang)
        {
            try
            {
                makhuyenmaimoi = getID();
                if (khuyenmaitanghang.MaKhuyenMaiTangHang == makhuyenmaimoi)
                {
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region Sửa
        void Sua()
        {
            try
            {
                khuyenmaitanghang = XuLyObject("Update");
                chitietkhuyenmaitanghang = (Entities.ChiTietKhuyenMaiTangHang[])dgvHienThi.DataSource;
                // check object
                if (khuyenmaitanghang == null)
                {
                    return;
                }
                // check object
                if (chitietkhuyenmaitanghang == null || chitietkhuyenmaitanghang.Length == 0)
                {
                    return;
                }
                // check conflic insert
                if (!CheckConflicUpdate())
                {
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check insert
                khuyenmaitanghang.TaiKhoan = UtilitiesVariable.User;
                // check insert details
                foreach (Entities.ChiTietKhuyenMaiTangHang item in chitietkhuyenmaitanghang)
                {
                    item.HanhDong = "Insert";
                }
                khuyenmaitanghang.ChiTietKhuyenMaiTangHang = chitietkhuyenmaitanghang;
                if (Common.StaticVariable.InterfaceObject.InsertUpdateKhuyenMaiTangHang(khuyenmaitanghang) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG15(khuyenmaitanghang.MaKhuyenMaiTangHang), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    Common.StaticVariable.InterfaceObject.DeleteKhuyenMaiTangHang(khuyenmaitanghang);
                    return;
                }

                checkClose = true;
                khuyenmaitanghang = null;
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Check Conflic Update
        public bool CheckConflicUpdate()
        {
            try
            {
                Entities.KhuyenMaiTangHang[] mangchitiet = new frmQuanLy().HienThi(Entitystatic.date);
                for (int i = 0; i < mangchitiet.Length; i++)
                {
                    if (mangchitiet[i].MaKhuyenMaiTangHang == temp.MaKhuyenMaiTangHang)
                    {
                        return Check(mangchitiet[i]);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        public bool Check(Entities.KhuyenMaiTangHang khuyenmaitanghang)
        {
            bool kt = true;
            if (temp.TenDotKhuyenMai != khuyenmaitanghang.TenDotKhuyenMai)
            {
                txtTenDotKhuyenMai.Text = temp.TenDotKhuyenMai = khuyenmaitanghang.TenDotKhuyenMai;
                kt = false;
            }
            return kt;
        }
        #endregion

        #endregion

        #region thoát
        public void Thoat()
        {
            this.Close();
        }
        #endregion

        #region check khuyen mai
        bool CheckNgayKhuyenMaiTangHang()
        {
            try
            {
                foreach (Entities.KhuyenMaiTangHang item in tempcheck)
                {
                    if (item.NgayBatDau.Date == Convert.ToDateTime(dtpkNgayBatDau.Text).Date && item.NgayKetThuc.Date == Convert.ToDateTime(dtpkNgayKetThuc.Text).Date)
                    {
                        return false;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        string makhuyenmaitrung = "";
        bool CheckHangHoaKhuyenMai()
        {
            try
            {
                foreach (Entities.KhuyenMaiTangHang item in tempcheck)
                {
                    if (item.GiaTriTrangThai == "Đang sử dụng" || item.GiaTriTrangThai == "Sẽ sử dụng" && item.MaKhuyenMaiTangHang != item.MaKhuyenMaiTangHang)
                    {
                        // check chi tiết ở list view
                        if (chitietkhuyenmaitanghang != null)
                        {
                            foreach (Entities.ChiTietKhuyenMaiTangHang item1 in chitietkhuyenmaitanghang)
                            {
                                if (item.MaKhuyenMaiTangHang == item1.MaKhuyenMaiTangHang)
                                {
                                    if (item1.MaHangKhuyenMai == txtMaHangHoaKM.Text && item1.SoLuongKhuyenMai.ToString() != txtSoLuongKM.Text)
                                    {
                                        return false;
                                    }
                                }
                            }
                        }
                        // check chi tiết ở database
                        foreach (Entities.ChiTietKhuyenMaiTangHang item1 in chitiettempcheck)
                        {
                            if (item.MaKhuyenMaiTangHang == item1.MaKhuyenMaiTangHang)
                            {
                                if (item1.MaHangKhuyenMai == txtMaHangHoaKM.Text && item1.SoLuongKhuyenMai.ToString() != txtSoLuongKM.Text)
                                {
                                    makhuyenmaimoi = item1.MaKhuyenMaiTangHang;
                                    return false;
                                }
                            }
                        }

                    }
                }
                return true;
            }
            catch
            {
                return false;

            }
        }
        #endregion

        #region event
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            btnGhiLai.Click += new EventHandler(btnGhiLai_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            this.FormClosing += new FormClosingEventHandler(frmXuLy_FormClosing);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
            txtMaHangHoaDuocKM.KeyDown += new KeyEventHandler(txtMaHangHoaDuocKM_KeyDown);
            txtMaHangHoaKM.KeyDown += new KeyEventHandler(txtMaHangHoaKM_KeyDown);
            btnThemGV.Click += new EventHandler(btnThemGV_Click);
            txtSoLuongDuocKM.TextChanged += new EventHandler(txtSoLuongDuocKM_TextChanged);
            txtSoLuongKM.TextChanged += new EventHandler(txtSoLuongKM_TextChanged);
        }

        void txtSoLuongKM_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int sl = int.Parse(txtSoLuongKM.Text);
                if (sl <= 0)
                    txtSoLuongKM.Text = ""; 
            }
            catch
            {
                txtSoLuongKM.Text = "";
            }
        }

        void txtSoLuongDuocKM_TextChanged(object sender, EventArgs e)
        {
            try
            {
               int sl = int.Parse(txtSoLuongDuocKM.Text);
                if(sl <=0)
                    txtSoLuongDuocKM.Text = "";
            }
            catch
            {
                txtSoLuongDuocKM.Text = "";
            }
        }

        void btnThemGV_Click(object sender, EventArgs e)
        {
            if (ValidateDetails())
            {
                mahanghoakm = txtMaHangHoaKM.Text;
                mahanghoaduockm = txtMaHangHoaDuocKM.Text;
                ThemHangKhuyenMai();
                txtMaHangHoaDuocKM.Text = txtMaHangHoaKM.Text = "<F4 - Tra cứu>";
                txtSoLuongDuocKM.Text = txtSoLuongKM.Text = string.Empty;
                lbTenHangHoaKM.Text = lbTenHangHoaDuocKM.Text = string.Empty;
                txtMaHangHoaKM.Focus();
            }
        }

        void txtMaHangHoaKM_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.F4)
                {
                    TimKiem.frmHangHoa timkiem = new TimKiem.frmHangHoa();
                    timkiem.ShowDialog();
                    if (TimKiem.frmHangHoa.selectedItem != null)
                    {
                        txtMaHangHoaKM.Text = TimKiem.frmHangHoa.selectedItem.MaHangHoa;
                        lbTenHangHoaKM.Text = TimKiem.frmHangHoa.selectedItem.TenHangHoa;
                        TimKiem.frmHangHoa.selectedItem = null;
                        txtSoLuongKM.Focus();
                    }
                }
            }
            catch
            {
            }
        }

        void txtMaHangHoaDuocKM_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F4)
            {
                try
                {
                    TimKiem.frmHangHoa timkiem = new TimKiem.frmHangHoa();
                    timkiem.ShowDialog();
                    if (TimKiem.frmHangHoa.selectedItem != null)
                    {
                        txtMaHangHoaDuocKM.Text = TimKiem.frmHangHoa.selectedItem.MaHangHoa;
                        lbTenHangHoaDuocKM.Text = TimKiem.frmHangHoa.selectedItem.TenHangHoa;
                        txtSoLuongDuocKM.Focus();
                        TimKiem.frmHangHoa.selectedItem = null;
                    }
                }
                catch
                {
                }
            }
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            XoaHangKhuyenMai();
        }
        int i = 0;
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        void frmXuLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
            else
            {
                GetData(kiemtra);
            }
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnGhiLai_Click(object sender, EventArgs e)
        {
            GhiLai();
        }
        #endregion
    }
}
#endregion